package com.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.system.enroll.model.Student;
import com.system.enroll.service.StudentService;

@Component("studentBean")
@Scope("session")
public class StudentBean {

	@Autowired
	StudentService studentService;
	
	public Integer id;
	public String fname;
	public String lname;
	public String email;
	public String phone;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFname() {
		return fname;
	}

	public void setFname(String fname) {
		this.fname = fname;
	}

	public String getLname() {
		return lname;
	}

	public void setLname(String lname) {
		this.lname = lname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	// get all student data from database
	public List<Student> getStudentList() {
		return studentService.findAllStudent();
	}
	
	
	//add a new Student data into database
		public String addStudent(){
			
			Student student = new Student();
			student.setFname(getFname());
			student.setLname(getLname());
			student.setEmail(getEmail());
			student.setPhone(getPhone());
			
			studentService.save(student);
			
			clearForm();
			
			return "";
		}
		
		
		public String deleteStudent(Student student) {
			 
			studentService.delete(student);
			return null;
		}
		
		
		
	//clear form values
		private void clearForm(){
			setFname("");
			setLname("");
			setPhone("");
			setEmail("");
		}
	

}
